<?php
/**
 * This file contains the job controller to manage jobs of resque.
 *
 * @author Vincent.Hou <vincenthou@augmentum.com.cn>
 */

/**
 * JobController class impletement the management and monitoring of resque jobs.
 *
 * @package system\resque\controllers
 * @author Vincent.Hou <vincenthou@augmentum.com.cn>
 *
 */
class JobController extends ResqueBaseController
{
    public $defaultAction = 'default';

    public function actionDefault()
    {
        $job = Yii::app()->request->getPost('Job');
        $this->loadResource();
        if(isset($job) && !empty($job['QueueName']) && !empty($job['JobClassName']))
        {
            $token = $this->selectEnqueueType($job);
            sleep(2);

            $job['Status'] = Yii::app()->resque->status($token);
            $job['JobToken'] = ('string' == gettype($token)) ? $token : CJSON::encode($token);
            $this->render('status', array('job' => $job));
        }
        else
        {
            $requestToken = $this->generateRequestToken();
            $this->render('index', array('requestToken' => $requestToken));
        }
    }

    public function actionCancel()
    {
        $job = Yii::app()->request->getPost('Job');
        $this->loadResource();
        if(isset($job) && !empty($job['QueueName']) && !empty($job['JobClassName']))
        {
            $args = !empty($job['Arguments']) ? CJSON::decode($job['Arguments']) : array();
            Yii::app()->resque->cancelDelayedJob(CJSON::decode($job['JobToken']), $job['QueueName'], $job['JobClassName'], $args);
            sleep(2);

            $job['Status'] = 'Check the redis if it is canceled';
            $this->render('status', array('job' => $job));
        }
        else
        {
            $requestToken = $this->generateRequestToken();
            $this->render('cancel', array('requestToken' => $requestToken));
        }
    }

    /**
     * Select the enqueueing type based on the job paramters, and enqueue the job
     *
     * @param $job
     */
    private function selectEnqueueType($job)
    {
        $token = null;
        $queueName = $job['QueueName'];
        $jobClassName = $job['JobClassName'];
        $args = !empty($job['Arguments']) ? CJSON::decode($job['Arguments']) : array();
        $trackStatus = isset($job['TrackStatus']);

        if (empty($job['DelayedTime']) && empty($job['ExecutedAt']))
        {
            $token = Yii::app()->resque->createJob($queueName, $jobClassName, $args, $trackStatus);
        }
        else
        {
            if (!empty($job['DelayedTime']))
            {
                $in = (int) $job['DelayedTime'];
                $token = Yii::app()->resque->enqueueJobIn($in, $queueName, $jobClassName, $args);
            }
            else if (!empty($job['ExecutedAt']))
            {
                $at = new DateTime($job['ExecutedAt']);
                $token = Yii::app()->resque->enqueueJobAt($at, $queueName, $jobClassName, $args);
            }
        }
        return $token;
    }
}